from django.shortcuts import render
from django.http.response import JsonResponse
from django.core.mail import send_mail
import random
import string
from .models import CaptchaModel

def loginView(request):
    content = {

    }
    return render(request, 'author/login.html', context=content)


def registerView(request):
    content = {

    }
    return render(request, 'author/register.html', context=content)


def pub_blogView(request):
    content = {

    }
    return render(request, 'author/pub_blog.html', context=content)


# 发送验证码
def send_email_captcha(request):
    email = request.GET.get('email')
    if not email:
        return JsonResponse({'status': 400, 'message': '请输入邮箱！'})
    else:
        captcha = ''.join(random.sample(string.digits, 4))
        CaptchaModel.objects.update_or_create(email=email, defaults={'captcha': captcha})
        send_mail('@F博客', f'您的验证码是：{captcha}', '625118310@qq.com', [email])
        return JsonResponse({'status': 400, 'message': '邮箱验证码发送成功！'})



